package com.example.mindadinner.net.response;

import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import com.example.mindadinner.net.NetRequest;
import com.example.mindadinner.net.NetResponse;
import com.example.mindadinner.net.request.PostRequest;


public  class PostResponse extends NetResponse {
	private static final long serialVersionUID = 1L;
	private HttpClient httpClient;

	@Override
	public <RES extends NetResponse> RES getResponseFromRequest(
			NetRequest request) throws ClientProtocolException, IOException {
		HttpPost httpPost = new HttpPost(request.getNetUrl());
		
		 HttpEntity entity = new UrlEncodedFormEntity(((PostRequest)request).getParams(), HTTP.UTF_8); //设置字符集
		 httpPost.setEntity(entity); // 设置参数实体
		HttpResponse httpResp = createHttpClient().execute(httpPost); // 获取HttpResponse实例
		if (httpResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 响应通过
			String httpresult = EntityUtils.toString(httpResp.getEntity(),
					"UTF-8");
			
		}
		return (RES) this;
	}
	

	public HttpClient getHttpClient() {
		return httpClient;
	}

	private HttpClient createHttpClient() {
		if (httpClient != null) {
			return httpClient;
		}
		httpClient= new DefaultHttpClient();
		return httpClient;
	}

}
